#include"Dialog.h"
Dialog::Dialog()
{
	TabIndex = 0;
}
/*
 * Argument Description:
 *     1. title : Title of the Dialog
 *     2. w     : Width of the Dialog usually 45
 *     3. h     : Height of the Dialog usually 8, 12 & 16 for 1, 2, & 3 nText
 *     4. nText : Number of TextBoxes to be present
 *     5. txtTitle : 2D char array to store the list of TextBox titles
 */
Dialog::Dialog(char *title, int w, int h, int nText, char **txtTitle) : Window(title, w, h)
{
	int i;
	TabIndex = 0;
	NoOfTextBoxes = nText;
	MaxTab = NoOfTextBoxes + 2;
	for ( i = 0; i < NoOfTextBoxes; i++ )
		Textbox[i].createTextBox(txtTitle[i], w - 11, 1);
	Ok.createButton("Ok", 8, 1);
	Cancel.createButton("Cancel", 8, 1);
}
Dialog::~Dialog()
{
	int i;
	for ( i = 0; i < NoOfTextBoxes; i++ )
		Textbox[i].TextBox::~TextBox();
	Ok.Button::~Button();
	Cancel.Button::~Button();
}
/****************************************************************************
 *  Name       : createDialog
 *  Parameters : Title of the Dialog Window
 *               Width of the Dialog Window (Best results with 45)
 *               Height of the Dialog Window (8, 12 & 16 for 1, 2 & 3 TextBoxes)
 *               Number of TextBoxes in the Dialog
 *               Titles of the TextBoxes (2D Array of char)
 *  Returns    : Nothing
 *  Purpose    : Reinitialize an existing Dialog with new Values.
 ****************************************************************************/
void Dialog::createDialog(char *title, int w, int h, int nText, char **txtTitle)
{
	int i;
	TabIndex = 0;
	NoOfTextBoxes = nText;
	MaxTab = NoOfTextBoxes + 2;
	createWindow(title, w, h);
	for ( i = 0; i < NoOfTextBoxes; i++ )
		Textbox[i].createTextBox(txtTitle[i], w - 11, 1);
	Ok.createButton("Ok", 8, 1);
	Cancel.createButton("Cancel", 8, 1);
}
/****************************************************************************
 *  Name       : getDialogRect
 *  Parameters : None
 *  Returns    : Rectangle
 *  Purpose    : Returns the Rectangle of the Dialog Window.
 ****************************************************************************/
Rectangle Dialog::getDialogRect()
{
	return getWindowRect();
}
/****************************************************************************
 *  Name       : getText
 *  Parameters : Index of the TextBox
 *  Returns    : String containing the content of that TextBox
 *  Purpose    : To get the value of a TextBox present in the Dialog
 ****************************************************************************/
inline char *Dialog::getText(int index)
{
	if ( index < NoOfTextBoxes )
		return Textbox[index].getText();
	return "\27";
}
/****************************************************************************
 *  Name       : getLength
 *  Parameters : Index of the TextBox
 *  Returns    : Length of the Text contained by that TextBox
 *  Purpose    : To get the length of the string in a particular TextBox.
 ****************************************************************************/
inline int Dialog::getLength(int index)
{
	if ( index < NoOfTextBoxes )
		return Textbox[index].getLength();
	return 0;
}
/****************************************************************************
 *  Name       : setXY
 *  Parameters : X position (Best with 15)
 *               Y position (Best with 5)
 *  Returns    : Nothing
 *  Purpose    : To set the coordinates of the Rectangle to display the
 *               Dialog and all its members such as the Buttons and TextBoxes.
 ****************************************************************************/
void Dialog::setXY(int x, int y)
{
	int i, off;
	Rectangle w;
	Window::setXY(x, y);
	for ( i = 0; i < NoOfTextBoxes; i++ )
		Textbox[i].setXY(x + 4, y + (i << 2) + 2); // y + 2, y + 6, y + 10 etc. ( y + (2 + (4 * i)))
	w = getWindowRect();
	off = ((w.right - w.left) - 16) / 3;
	Ok.setXY(x + off, y + (i << 2) + 2);
	off <<= 1;
	off += 8;
	Cancel.setXY(x + off, y + (i << 2) + 2);
}
/****************************************************************************
 *  Name       : showDialog
 *  Parameters : None
 *  Returns    : Nothing
 *  Purpose    : To display the Dialog on the screen.
 ****************************************************************************/
void Dialog::showDialog()
{
	int i;
	showWindow();
	for ( i = 0; i < NoOfTextBoxes; i++ )
		Textbox[i].showTextBox();
	Ok.showButton();
	Cancel.showButton();
}
/****************************************************************************
 *  Name       : handleDialog
 *  Parameters : None
 *  Returns    : Success value (1 on success and 0 on failure)
 *  Purpose    : To handle events occuring in the Dialog which is a sum
 *               total of all the events occuring in its members.  Keeps
 *               track of the TabIndex and passes control to appropriate
 *               handlers.
 ****************************************************************************/
int Dialog::handleDialog()
{
	char ch;
	while ( 1 )
	{
		if ( TabIndex  < NoOfTextBoxes ) // If one of the TextBoxes
		{
			ch = Textbox[TabIndex].handleTextBox();
			if ( ch == TAB )
				TabIndex++;
			else if ( ch == ESC )
				return 0;
		} // Ok or Cancel
		else if ( TabIndex < MaxTab )
		{
			if ( TabIndex == MaxTab - 2 ) // Ok Button
			{
				ch = Ok.handleButton();
				if ( ch == TAB )
					TabIndex++;
				else if ( ch == ENTER )
					return 1;
				else if ( ch == ESC )
					return 0;
			} // Endif (Ok)
			else if ( TabIndex == MaxTab - 1 ) // Cancel Button
			{
				ch = Cancel.handleButton();
				if ( ch == TAB )
					TabIndex = 0;
				else if ( ch == ESC || ch == ENTER )
					return 0;
			} // Endif (Cancel)
		} // Endif
	} // Endwhile (Infinite)
}
// Class Dialog Ends
